<template>
  <!-- 我的餐厅 -->
  <view class="myRestaurant">
    <view class="content">
      <view class="content-item">
        <view class="wrapper-content">
          <scroll-view
            scroll-y="true"
            @scrolltolower="scrollBottom"
            @scroll="scrollHeight"
            :style="[
              {
                height: '100%',
              },
            ]"
            v-if="dataSource.length"
          >
            <view
              class="wrapper-item"
              v-for="(v, i) in dataSource"
              :key="i"
              :data-url="
                '/guideInfo/myRestaurant/regulationsCatalogue?title=' + v.name
              "
              @click="handleClickRouter"
            >
              <view class="items">
                <view class="lt">
                  <view class="line-hidden name">
                    {{ v.restaurantName }}
                  </view>
                  <view class="clause"> 餐厅规模：{{ v.scale_dictText }} </view>
                  <view class="clause" v-if="v.auditStatus == 1">
                    提交时间：{{ v.createTime }}
                  </view>
                  <view class="clause" v-else-if="v.auditStatus == 2">
                    通过时间：{{ v.auditTime }}
                  </view>
                  <view class="clause" v-else>
                    拒绝时间：{{ v.auditTime }}
                  </view>
                </view>
                <view class="rt">
                  <image
                    :src="
                      v.auditStatus == 1
                        ? baseImgUrl + 'self-state1.png'
                        : v.auditStatus == 3
                        ? baseImgUrl + 'self-state2.png'
                        : baseImgUrl + 'self-state3.png'
                    "
                    mode="aspectFill"
                  ></image>
                </view>
              </view>

              <view class="down">
                <!-- auditStatus     1.审核中 2.审核通过 3.审核失败 -->
                <block v-if="v.auditStatus != 1">
                  <view class="rt" @click="reAmendHandle(v)">
                    <image
                      :src="
                        v.auditStatus == 3
                          ? baseImgUrl + 'business2.png'
                          : baseImgUrl + 'business5.png'
                      "
                      mode="aspectFill"
                    ></image>
                    {{ v.auditStatus == 3 ? "重新提交" : "更换绑定" }}
                  </view>
                  <view
                    class="rt"
                    :data-url="`/selfInfo/deleteRestaurant/deleteRestaurant?id=${v.id}`"
                    @click="handleClickRouter"
                    v-show="v.auditStatus == 3"
                  >
                    <image
                      :src="
                        v.auditStatus == 3 ? baseImgUrl + 'business3.png' : ''
                      "
                      mode="aspectFill"
                    ></image>
                    {{ v.auditStatus == 3 ? "删除" : "" }}
                  </view>
                </block>
                <block v-else><view></view></block>
                <view
                  class="rt"
                  :data-url="`/selfInfo/myRestaurant/restaurantDetails?id=${v.id}`"
                  @click="handleClickRouter"
                >
                  <image
                    :src="baseImgUrl + 'business1.png'"
                    mode="aspectFill"
                  ></image>
                  查看详情
                </view>
              </view>
            </view>
          </scroll-view>
          <view v-if="!dataSource.length">
            <u-empty
              mode="list"
              text="暂无内容！"
              :icon="baseImgUrl + 'no-icon2.png'"
            >
            </u-empty>
          </view>
        </view>
      </view>
    </view>

    <view class="btns">
      <button
        class="submit-btn"
        type="primary"
        :data-url="'/selfInfo/myRestaurant/addRestaurant'"
        @click="handleClickRouter"
        icon="map"
        style="width: 100%"
      >
        + 添加餐厅
      </button>
    </view>
  </view>
</template>

<script>
import { restaurantPage } from "@/api/myself.js";
import { handleClickRouter, Toast } from "@/utils/util.js";

export default {
  name: "myRestaurant",
  data() {
    return {
      baseImgUrl: this.$baseImgUrl,
      scrollTop: 0,
      form: {},
      errorType: ["none"],
      dataSource: [],
      pageBottom: false,
      pageConfig: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  onLoad() {
    this.scrollTop = 0;
    uni.removeStorageSync("scrollTop");
  },
  onShow() {
    this.scrollTop = uni.getStorageSync("scrollTop") || 0;

    this.pageConfig.pageNo = 1;
    this.dataSource = [];
    this.getList();
    // uni.$on("myRestaurant", (data) => {
    //   this.pageConfig.pageNo = 1;
    //   this.dataSource = [];
    //   this.getList();
    // });
  },
  methods: {
    handleClickRouter,
    scrollHeight(e) {
      uni.setStorageSync("scrollTop", e.detail.scrollTop);
    },
    scrollBottom() {
      let current = (this.pageConfig.pageNo += 1);
      let pages = Math.ceil(this.pageConfig.total / this.pageConfig.pageSize);
      if (current <= pages) {
        this.getList();
      } else {
        this.pageBottom = true;
      }
    },
    reAmendHandle(item) {
      if (item.auditStatus == 1) {
        uni.navigateTo({
          url: "/selfInfo/myRestaurant/addRestaurant",
        });
      } else if (item.auditStatus == 2) {
        uni.navigateTo({
          url: `/selfInfo/deleteRestaurant/deleteRestaurant?id=${item.id}`,
        });
      } else if (item.auditStatus == 3) {
        uni.navigateTo({
          url: `/selfInfo/myRestaurant/addRestaurant?data=${encodeURIComponent(
            JSON.stringify(item)
          )}`,
        });
      }
    },
    getList() {
      let data = {
        pageNo: this.pageConfig.pageNo,
        pageSize: this.pageConfig.pageSize,
      };

      restaurantPage(data).then((res) => {
        this.dataSource = this.dataSource.concat(res.result.records || []);
        this.pageConfig.total = res.result.total;
      });
    },
  },
};
</script>

<style lang="scss">
.myRestaurant {
  flex: 1;
  background-color: #f9f9f9;

  .content {
    margin: 0 auto;
    margin-top: 20rpx;
    margin-bottom: 20rpx;
    border-radius: 8rpx;
    width: calc(100% - 35rpx);
    height: calc(100vh - 84px);
    overflow-y: auto;

    .content-item {
      background: #f9f9f9;
    }
  }

  .wrapper-content {
    height: calc(100vh - 84px);

    .wrapper-item {
      padding: 30rpx;
      background: #ffffff;
      margin-bottom: 20rpx;

      .items {
        display: flex;
        .lt {
          width: 480rpx;
          max-width: 480rpx;
          .name {
            font-size: 30rpx;
            font-family: PingFang SC;
            font-weight: bold;
            margin-bottom: 17rpx;
            margin-top: 20rpx;
          }

          .clause {
            margin-bottom: 17rpx;
            font-size: 26rpx;
            font-family: PingFang SC;
            font-weight: 400;
            color: #a1a1a1;
          }
        }

        .rt {
          width: 150rpx;
          height: 125rpx;
          margin-top: 30rpx;
          margin-left: 30rpx;
          image {
            width: 100%;
            height: 100%;
          }
        }
      }

      .down {
        display: flex;
        justify-content: space-between;
        margin-top: 20rpx;
        padding-top: 20rpx;
        border-top: 1px solid #dedcdc;
        text-align: right;

        .rt {
          font-size: 30rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;
          image {
            margin-right: 5px;
            margin-bottom: -3rpx;
            width: 29rpx;
            height: 32rpx;
          }
        }
      }
    }
  }
}
</style>
